const { db } = require('../db');

exports.getUserById = async (userId) => {
    const sql = "select * from users where id=?";
    return new Promise((resolve, reject) => {
        db.query(sql, userId, (err, results) => {
            if (err) {
                reject(err);
            } else {
                const result = results[0];
                if (result) {
                    resolve(results[0]);
                }
            }
        });
    });
}

exports.getUserByEmail = async (email) => {
    const sql = "select * from users where email=?";
    return new Promise((resolve, reject) => {
        db.query(sql, email, (err, results) => {
            if (err) {
                reject(err);
            } else {
                const result = results[0];
                if (result) {
                    resolve(results[0]);
                }
            }
        });
    });
}

exports.setValueByUserId = async (userId, key, value) => {
    const sql = "update users set " + key + "=? where id=?";
    return new Promise((resolve, reject) => {
        db.query(sql, [value, userId], (err, results) => {
            if (err) {
                reject(err);
            } else {
                resolve(results);
            }
        });
    });
}